package openbang.account.web.portal.client;

import openbang.account.service.intf.AccountService;
import openbang.account.service.vo.AccountServiceEnum;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;


/**
 * 找回密码控制 
 * @author aibo zeng
 *
 */
public class FindPwd implements EntryPoint {
	private TextBox email = null;
	private Button  findPwdBtn = null;
	private HTML emailTitleLabel = new HTML();

	
	private void bindUI(){
		findPwdBtn = Button.wrap(Document.get().getElementById("findPwdBtn"));
		email = TextBox.wrap(Document.get().getElementById("emailTxt"));
		RootPanel.get("emailTitle").add(emailTitleLabel);
	}	
	@Override
	public void onModuleLoad() {
		bindUI();

/*	    只有一个文本框，没有必要实时检查
 *    	email.addBlurHandler(new BlurHandler() {
			public void onBlur(BlurEvent event) {
				//焦点离开，检查email是否已经注册
				doFindEmail();
			}
		});
*/
		findPwdBtn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				doFindPwd();
			}
		});
		//加载完，按钮即可用
		findPwdBtn.setEnabled(true);
		
	}
/*	暂时在录入过程中，不进行检查
	public void doFindEmail(){
		if(email.getText()==null || email.getText().trim().length()<=0){
			email.setText("");
			Window.alert("请录入邮箱");
			return;
		}
			//后台检查
			AccountService.Util.getInstance().findEmail4FindPwd(email.getText(), new AsyncCallback<Integer>(){
				public void onSuccess(Integer result) {
					//Window.alert(result);
					if(result==AccountServiceEnum.OK.getCode()){
						//找到
						findPwdBtn.setEnabled(true);
						emailTitleLabel.setHTML("<font color=\"red\">存在该邮箱，可以找回</font>");
					}
					else if(result==AccountServiceEnum.ERR_EMAIL.getCode()){
						findPwdBtn.setEnabled(false);
						emailTitleLabel.setHTML("<font color=\"blue\">不存在该邮箱</font>");
					}
					else if(result==AccountServiceEnum.ERR_FINDPWD.getCode()){
						findPwdBtn.setEnabled(false);
						emailTitleLabel.setHTML("<font color=\"blue\">Google管理密码,本站不能找回</font>");						
					}
					else{
						Window.alert(AccountServiceEnum.getDesc(result));
					}
				}
				public void onFailure(Throwable caught) {
					Window.alert(caught.getMessage());
				}				
			});
		return;		
	}
*/
	public void doFindPwd(){
		if(email.getText()==null || email.getText().trim().length()<=0){
			Window.alert("请录入邮箱");
			email.setText("");
			return;
		}
		findPwdBtn.setEnabled(false);
		emailTitleLabel.setHTML("<font color=\"blue\">处理中...</font>");
		emailTitleLabel.setStyleName("contentTxt");
		AccountService.Util.getInstance().applyFindPwd(email.getText(), new AsyncCallback<Integer>(){
			public void onSuccess(Integer result) {
				if(result==AccountServiceEnum.OK.getCode()){
					emailTitleLabel.setHTML("<font color=\"blue\">处理完毕,请去您的邮箱系统收取邮件</font>");
					Window.alert("申请找回密码成功,请过几分钟后收取邮件 email="+email.getText());
					//email.setText("");
				}
				else{
					emailTitleLabel.setHTML("<font color=\"blue\">找回失败:"+AccountServiceEnum.getDesc(result)+"</font>");
				}
			}
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
				findPwdBtn.setEnabled(true);
			}				
		});
	}
	
}
